package cn.darkhorse.basic.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.darkhorse.basic.mapper.QmsMoistureTesterMapper;
import cn.darkhorse.basic.domain.QmsMoistureTester;
import cn.darkhorse.basic.service.IQmsMoistureTesterService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 回潮测试仪Service业务层处理
 *
 * @author ytq
 * @date 2024-08-12
 */
@Service
public class QmsMoistureTesterServiceImpl extends ServiceImpl<QmsMoistureTesterMapper, QmsMoistureTester> implements IQmsMoistureTesterService
{
    @Autowired
    private QmsMoistureTesterMapper qmsMoistureTesterMapper;

    /**
     * 查询回潮测试仪
     *
     * @param id 回潮测试仪主键
     * @return 回潮测试仪
     */
    @Override
    public QmsMoistureTester selectQmsMoistureTesterById(Long id) {
        return getById(id);
    }

    /**
     * 查询回潮测试仪列表
     *
     * @param qmsMoistureTester 回潮测试仪
     * @return 回潮测试仪
     */
    @Override
    public List<QmsMoistureTester> selectQmsMoistureTesterList(QmsMoistureTester qmsMoistureTester) {
        QueryWrapper<QmsMoistureTester> queryWrapper = new QueryWrapper<>();
        if (qmsMoistureTester.getCode() != null){
            queryWrapper.like("code",qmsMoistureTester.getCode());
        }
        if (qmsMoistureTester.getModel() != null){
            queryWrapper.like("model",qmsMoistureTester.getModel());
        }
        queryWrapper.orderByDesc("create_time");
        return qmsMoistureTesterMapper.selectList(queryWrapper);
    }

    /**
     * 新增回潮测试仪
     *
     * @param qmsMoistureTester 回潮测试仪
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveQmsMoistureTester(QmsMoistureTester qmsMoistureTester) {
        String code = qmsMoistureTester.getCode();
        if (code != null){
            Long count;
            if (qmsMoistureTester.getId()!=null){
                count = lambdaQuery().eq(QmsMoistureTester::getCode, code).ne(QmsMoistureTester::getId, qmsMoistureTester.getId()).count();
            }else {
                count = lambdaQuery().eq(QmsMoistureTester::getCode, code).count();
            }
            if (count>0){
                throw new RuntimeException("编号重复");
            }
        }
        return saveOrUpdate(qmsMoistureTester);
    }


    /**
     * 批量删除回潮测试仪
     *
     * @param ids 需要删除的回潮测试仪主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteQmsMoistureTesterByIds(Long[] ids) {
        return removeBatchByIds(Arrays.asList(ids));
    }

    /**
     * 删除回潮测试仪信息
     *
     * @param id 回潮测试仪主键
     * @return 结果
     */
    @Override
    public boolean deleteQmsMoistureTesterById(Long id) {
        return removeById(id);
    }
}
